//
//  LSZDirectoryDetailsViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/20.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZDirectoryDetailsViewController.h"

@interface LSZDirectoryDetailsViewController ()<UIWebViewDelegate>
@property(nonatomic,assign) NSInteger isOkCollects;
@end

@implementation LSZDirectoryDetailsViewController
-(void)clickButCollection
{
    if (self.isOkCollects == 0) {
        [LSZRequestData  initTheArticleCollects:self.productID :^(BOOL isOk, NSString * str, NSDictionary * diction) {
            NSLog(@"收藏>>>%@",diction);
            if (isOk == YES) {
                if (self.isOkCollects == 0) {
                    self.isOkCollects = 1;
                    [self initWithtring:@"收藏成功!"];
                }else{
                    [self initWithtring:str];
                }
            }else{
                [self initWithtring:str];
            }
        }];
    }else{
        [self initWithtring:@"已收藏!"];
    }
}
-(void)addDirectoryDetailsView
{
    LSZArticleDetailsModel * model = [[LSZArticleDetailsModel alloc]init];
    NSArray * arrayExisting = [WHCSqlite query:[model class]];
    NSString * string = @"0";
    NSString * productID = @"";
    NSString * url = @"";
    for ( LSZArticleDetailsModel * mg in arrayExisting) {
        if ([mg.id_id isEqualToString:self.productID]) {
            string = @"1";
            productID = [NSString stringWithFormat:@"%@",mg.id_id];
            url = [NSString stringWithFormat:@"%@",mg.url];
            break;
        }
    }
    if ([string isEqualToString:@"1"]) {
        NSLog(@"有数据");
    }else{
#pragma mark - 没有歌谱
        model.id_id = self.productID;
        model.url = self.url;
        model.imageViewStr = self.imageViewStr;
        model.titleStr = self.titleStr;
        model.publishTime = self.publishTime;
        [WHCSqlite insert:model];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isOkCollects = 0;
    self.title = @"00:00:20";
    [self addDirectoryDetailsView];
    UIButton * butRight = [[UIButton alloc]init];
    butRight.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT-22-10, 22, 22);
    [butRight setImage:[UIImage imageNamed:@"收藏_白"] forState:UIControlStateNormal];
    [butRight addTarget:self action:@selector(clickButCollection) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:butRight];
    
    UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    NSURLRequest *requers = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    webView.delegate = self;
    [webView loadRequest:requers];
    [self.view addSubview:webView];
    
}
-(void)addCountdown
{
    __block int timeout=20; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
//            dispatch_release(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.title = @"详情";
                if ([self.period isEqualToString:@"0"]||[self.period isEqualToString:@"20"]) {
                    [LSZRequestData completeDocumentation:self.period paperId:self.productID :^(BOOL isOk, NSString * stirng, NSString * sstr) {
                        
                    }];
                }
            });
        }else{
            NSString *strTime = [[NSString alloc]init];
            int seconds = timeout % 60;
            if (seconds<10) {
                strTime = [NSString stringWithFormat:@"00:00:0%ld",(long)seconds];
            }else{
                strTime = [NSString stringWithFormat:@"00:00:%ld",(long)seconds];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.title = strTime;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    //开始加载网页调用此方法
//    NSLog(@"123");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //网页加载完成调用此方法
    [self addCountdown];
}
@end
